package com.jxsd.edu.cn.struts2.daoImpl;

import java.util.List;

import com.jxsd.edu.cn.struts2.dao.TopicDao;
import com.jxsd.edu.cn.struts2.models.Topic;
import com.jxsd.edu.cn.tools.Page;
import com.jxsd.edu.cn.tools.PageBean;
import com.jxsd.edu.cn.tools.ResultBean;
import com.jxsd.edu.cn.tools.ResultList;
import com.jxsd.edu.cn.tools.Template;

public class TopicDaoImpl implements TopicDao {
	
	@Override
	public void addTopic(Topic topic) {
		String  sql="insert  into  topicview(Content,PostTime,UserID,Title,smallId)"+
                     " select  ?,?,?,?,smallId  from   smallCatagory  where  smallName=?";
		Object[] params={topic.getContent(),topic.getPostTime(),topic.getUserID(),topic.getTitle(),topic.getSmallName()};
		Template  template=new  Template();
		template.update(sql, params);
	}

	@Override
	public Topic getTopic(String topicId) 
	{
	    String  sql="select  topicID,content,postTime," +
	    		         "userID,title,nickName,Image,smallId,smallName  from  topicView  where  topicId=?";
	    Object[] params={topicId};
	    ResultBean<Topic>  bean=new  ResultBean<Topic>();
	    Topic  topic=null;
	    try {
			topic=bean.handler(sql, params, Topic.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return topic;
	}

	@Override
	public void deleteTopic(String topicId) {
		String  sql="delete  from  topic  where  topicId=?";
		Object[] params={topicId};
		Template  template=new  Template();
		template.update(sql, params);
	}

	@Override
	public Page<Topic> getPageTopics(PageBean<Topic>  pagebean,String smallId, String cityName) {
		String   sql="select   t.topicID,CONCAT('',DATEDIFF(curdate(),t.postTime))  as  'postTime'," +
				     "  t.userID,t.title,t.contentImage,count(r.replyId)  as  'replyNum',smallName  "+
                     "  from  topicview as  t,reply  as  r  where  smallId=?  and   cityName=?  and  t.topicID = r.TopicID  group  by  t.topicID";
		Object[]   params={smallId,cityName};
		
		for(Object s:params)
		{
			System.out.println("TopicDaoImpl:params:"+s);
		}
		
		
		ResultList<Topic>   hander=new   ResultList<Topic>();
		Page<Topic>  page=null;
		try {
			page=hander.pageHandler(pagebean, sql, params, Topic.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return page;
	}

	@Override
	public Page<Topic> getAllTopices(PageBean<Topic>  pagebean,String smallId)
	{
		String   sql="select   t.topicID,CONCAT('',DATEDIFF(curdate(),t.postTime))  as  'postTime'," +
			     "  t.userID,t.title,t.contentImage,count(r.replyId)  as  'replyNum',smallName  "+
                "  from  topicview as  t,reply  as  r  where  smallId=?  and   cityName=?  and  t.topicID = r.TopicID  group  by  t.topicID";
	    Object[]   params={smallId};
	    ResultList<Topic>  hander=new  ResultList<Topic>();
	    Page<Topic>   page=null;
	    try {
			page=hander.pageHandler(pagebean, sql, params, Topic.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return page;
	}

	@Override
	public Page<Topic> getPersonTopic(PageBean<Topic> pagebean, String userId) 
	{
		String   sql="select   topicID,title, postTime  from  topicview  where  userId=? ";
	   Object[]  params={userId};
	   ResultList<Topic>  hander=new  ResultList<Topic>();
	   Page<Topic>   page=null;
	   try {
		page=hander.pageHandler(pagebean, sql, params, Topic.class);
	} catch (Exception e) {
		e.printStackTrace();
	}
		return page;
	}

	@Override
	public List<Topic> queryNewlyTopic(String cityName) {
		List<Topic>  list=null;
		String  sql="select  topicID,title  from  topicview   where  cityName=?  order  by   postTime  limit 0,10;";
		Object[]  params={cityName};
		ResultList<Topic>  hander=new  ResultList<Topic>();
		try {
			list=hander.handler(sql, params, Topic.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

}
